<!--
 * @Author: your name
 * @Date: 2020-08-15 09:32:08
 * @LastEditTime: 2020-08-17 16:27:36
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vue_shop_shop\src\views\user\User.vue
-->
<template>
  <div>
    <h3>用户列表</h3>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索和添加区域 -->
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input v-model="queryInfo.query" placeholder="请输入关键字" clearable @clear='getUserlist'></el-input>
          <el-button  type="primary" icon="el-icon-search" @click="getUserlist">搜索</el-button>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="adddialogVisible=true">添加用户</el-button>
        </el-col>
      </el-row>
    </el-card>
    <el-table :data="userlist" border stripe>
      <el-table-column prop="username" label="姓名"></el-table-column>
      <el-table-column prop="email" label="邮箱"></el-table-column>
      <el-table-column prop="mobile" label="电话"></el-table-column>
      <el-table-column prop="role_name" label="角色"></el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.mg_state" @change="userStateChanged(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180px">
        <template slot-scope="scope">
          <!-- 修改 -->
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="handleEdit(scope.row)"></el-button>
          <!-- 删除 -->
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="handleDel(scope.row.id)"
          ></el-button>
          <!-- 分配角色 -->
          <el-tooltip class="item" effect="dark" content="分配角色" placement="top" :enterable="false">
            <el-button type="warning" icon="el-icon-setting" size="mini" @click="setRole(scope.row)"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <!-- size-change ----pagesize发生改变的时候所触发的事件
    current-change ----当前页码发生改变的时候所触发的事件-->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[2, 5, 10]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
    <!-- 添加用户的对话框 -->
    <el-dialog
    title="添加用户"
    :visible.sync="adddialogVisible"
    width="50%"
    >
    <!-- 对话框里面的form表单 -->
    <!-- 自己写去吧，小伙子 -->
    <el-input placeholder="随便写个东西"></el-input>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
    </span>
  </el-dialog>
   <!-- 分配角色的弹框 -->
  <el-dialog
    title="分配角色"
    :visible.sync="setRoleDialogVisible"
    width="50%"
    @close="hadleDialogClosed"
    >
    <div>
      <p>用户名:{{userInfo.username}}</p>
      <p>当前角色:{{userInfo.role_name}}</p>
      <p>分配新角色:
      <el-select v-model="selecttedRoleId" placeholder="请选择新的角色">
        <el-option
          v-for="item in roleList"
          :key="item.id"
          :label="item.roleName"
          :value="item.id">
        </el-option>
      </el-select>
         
      </p>
    </div>
    <span slot="footer" class="dialog-footer">
        <el-button @click="setRoleDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveRoleInfo">确 定</el-button>
      </span>
   </el-dialog>

  </div>
</template>

<script>
export default {
  data() {
    return {
      // 根据用户的关键字
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 2
      },
      // 请求数据存放的变量
      userlist: [],
      // 返回的数据一共有多少个
      total: 0,
      adddialogVisible : false,
      setRoleDialogVisible:false,
      // 保存的正在操作那个用户的信息
      userInfo:{},
      // 保存所有角色
      roleList:[],
      // 保存用户所选择角色的id
      selecttedRoleId:''
    }
  },
  created() {
    this.getUserlist()
  },
  methods: {
    async getUserlist() {
      // 发送请求列表数据
      const { data: res } = await this.$http.get('users', {
        params: this.queryInfo
      })
      // 如果说返回的状态异常，报错并返回
      if (res.meta.status !== 200) {
        return this.$message.error('获取列表失败')
      }
      // 返回的状态正常
      this.userlist = res.data.users
      this.total = res.data.total
      console.log(this.userlist)
      console.log(this.total)
    },
    handleSizeChange(newsize) {
      this.queryInfo.pagesize = newsize
      this.getUserlist()
    },
    handleCurrentChange(current) {
      this.queryInfo.pagenum = current
      this.getUserlist()
    },
    handleEdit(row) {
      console.log(row)
    },
    handleDel(id) {
      console.log(id)
    },
    async userStateChanged(row) {
      const { data: res } = await this.$http.put(
        `users/${row.id}/state/${row.mg_state}`
      )
      if (res.meta.status !== 200) {
        row.mg_state = !row.mg_state
        return this.$message.error('修改状态失败')
      }
      this.$message.success('更新状态成功')
    },
    async setRole (userinfo){
      console.log(userinfo)
       // 保存---用户的信息;
       this.userInfo = userinfo
       // 获取所有的角色,来方便下拉框进行使用
       const { data : res } = await this.$http.get('roles')
       if(res.meta.status !==200){
         return this.$message.error('获取角色失败')
       }
       this.roleList = res.data
       //需要把对话框显示出来
       this.setRoleDialogVisible = true
    },
    async saveRoleInfo (){
       // 先来进行判断用户是不是选中相应角色
       if(!this.selecttedRoleId){
         return this.$message.error('你个大傻子')
       }
       // 发送请求完成分配角色的操作
       const { data : res} = await this.$http.put(`users/${this.userInfo.id}/role`,{
         rid:this.selecttedRoleId
       })
       // 判断
       if(res.meta.status !==200){
         return this.$message.error('分配失败')
       }
       this.$message.success('分配角色成功')
       //重新处理
       this.getUserlist()
       this.setRoleDialogVisible=false
    },
    hadleDialogClosed(){
      //关闭对话框的时候啊啊啊
      this.selecttedRoleId= ''
      this.userInfo={}
    }
  }
}
</script>

<style lang="less" scoped>
.el-input {
  width: 150px;
}
</style>